﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text;
using LinQ_DTO;

namespace DAO
{
    public class HoaDonDAO
    {
        public List<shop_hoadon> LayDanhSachHoaDon()
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var hd = from c in EFashionDB.shop_hoadons select c;
            return hd.ToList();
        }

        public shop_hoadon LayHoaDonsTheoMa(int MaHoaDon)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            shop_hoadon hd = EFashionDB.shop_hoadons.Single(c => c.MaHoaDon == MaHoaDon);
            return hd;
        }

        public void XoaHoaDon(int MaHoaDon)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var hd = (from c in EFashionDB.shop_hoadons
                      where c.MaHoaDon == MaHoaDon
                      select c);
            EFashionDB.shop_hoadons.DeleteAllOnSubmit(hd);
            EFashionDB.SubmitChanges();
        }

        public void CapNhatHoaDon(shop_hoadon hdLinq)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            shop_hoadon hd = EFashionDB.shop_hoadons.Single(c => c.MaHoaDon == hdLinq.MaHoaDon);
            hd.NgayLap = hdLinq.NgayLap;
            hd.ThanhTien = hdLinq.ThanhTien;
            hd.UserId = hdLinq.UserId;
            hd.DiaChiGiaoHang = hdLinq.DiaChiGiaoHang;
            hd.TinhTrang = hdLinq.TinhTrang;

            EFashionDB.SubmitChanges();
        }

        public void ThemHoaDon(shop_hoadon hdLinq)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            EFashionDB.shop_hoadons.InsertOnSubmit(hdLinq);

            EFashionDB.SubmitChanges();
        }

        public int ThemHoaDon_LayID(shop_hoadon hdLinq)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            EFashionDB.shop_hoadons.InsertOnSubmit(hdLinq);

            EFashionDB.SubmitChanges();
            try
            {
                int id = hdLinq.MaHoaDon;
                return id;
            }
            catch (Exception ex)
            {
                return 0;
            }



        }

        /************************************************************************************************************        *
         * coder: TRAN KINH LY 
         ***********************************************************************************************************/
        public List<shop_hoadon> LayDanhSach_NgayTaoGiamDan()
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var list = from cmt in EFashionDB.shop_hoadons
                       orderby cmt.NgayLap descending
                       select cmt;

            return list.ToList();
        }

        public void CapNhatTinhTrangHoaDon(int id)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            shop_hoadon hd = EFashionDB.shop_hoadons.Single(c => c.MaHoaDon == id);
            hd.TinhTrang = true;

            EFashionDB.SubmitChanges();
        }

        public List<shop_hoadon> TimKiemHoaDonTheoThoiGian(DateTime date_start, DateTime date_end)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var list = from cmt in EFashionDB.shop_hoadons
                       where cmt.NgayLap >= date_start && cmt.NgayLap <= date_end
                       orderby cmt.NgayLap descending
                       select cmt;

            return list.ToList();
        }

        /************************************************************************************************************        *
         * coder: TRAN KINH LY 
         ***********************************************************************************************************/

    }
}
